package com.ws.shop.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import com.ws.shop.bean.DynamicConfig;
import com.ws.shop.bean.HtmlConfig;
import com.ws.shop.bean.MailConfig;

/**
 * 工具类 - 模板配置
 */

public class TemplateConfigUtil {
	
	public static final String CONFIG_FILE_NAME = "/WEB-INF/config/template.xml";// 模板配置文件名称
	public static final String DYNAMIC_NODE="/shop/dynamicConfig";
	public static final String HTMLCONFIG_NODE = "/shop/htmlConfig";
	public static final String MAILCONFIG_NODE = "/shop/mailConfig";
	public static final String filePath = "";
	 
	private static Map<String,DynamicConfig> dynamicConfigMap;//动态页面模板列表
	private static Map<String,HtmlConfig> htmlConfigMap;//静态页面模板列表
	private static Map<String,MailConfig> mailConfigMap;//邮件模板
	
	public static final Map<String,String> ARTICLE_TEMPLATE_MAP = new LinkedHashMap<String,String>();
	
	public static final Map<String,String> AUCTION_TEMPLATE_MAP = new LinkedHashMap<String,String>();
	
	static{
		ARTICLE_TEMPLATE_MAP.put("1", "/WEB-INF/template/article_content.html");
		ARTICLE_TEMPLATE_MAP.put("2", "/WEB-INF/template/article_tree_content.html");
		ARTICLE_TEMPLATE_MAP.put("3", "/WEB-INF/template/article_tree_navigation_content.html");
		
		AUCTION_TEMPLATE_MAP.put("0", "/WEB-INF/template/auction_detail_0.html");
		AUCTION_TEMPLATE_MAP.put("1", "/WEB-INF/template/auction_detail_1.html");
		AUCTION_TEMPLATE_MAP.put("2", "/WEB-INF/template/auction_detail_2.html");
	}
	
	/**
	 * 获取动态模板配置
	 * 
	 * @return DynamicConfig集合
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,DynamicConfig> getDynamicConfigMap() {
		if (dynamicConfigMap != null) {
			return dynamicConfigMap;
		}
		File configFile = null;
		Document document = null;
		try {
			String configFilePath = SpringUtil.getRealPath(CONFIG_FILE_NAME);
			configFile = new File(configFilePath);
			SAXReader saxReader = new SAXReader();
			document = saxReader.read(configFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Element htmlConfigElement = (Element)document.selectSingleNode(DYNAMIC_NODE);
		Iterator<Element> iterator = htmlConfigElement.elementIterator();
		dynamicConfigMap = new LinkedHashMap<String,DynamicConfig>();
	    while(iterator.hasNext()) {
	    	Element element = (Element)iterator.next();
	    	String description = element.element("description").getTextTrim();
	    	String templateFilePath = element.element("templateFilePath").getTextTrim();
	    	DynamicConfig dynamicConfig = new DynamicConfig();
	    	dynamicConfig.setName(element.getName());
	    	dynamicConfig.setDescription(description);
	    	dynamicConfig.setTemplateFilePath(templateFilePath);
	    	dynamicConfigMap.put(dynamicConfig.getName(),dynamicConfig);
	    }
		return dynamicConfigMap;
	}
	
	/**
	 * 根据动态模板配置名称获取DynamicConfig对象
	 * 
	 * @return DynamicConfig对象
	 */
	public static DynamicConfig getDynamicConfig(String name) {
		return getDynamicConfigMap().get(name);
	}
	
	/**
	 * 根据DynamicConfig对象读取模板文件内容
	 * 
	 * @return 模板文件内容
	 */
	public static String readTemplateFileContent(DynamicConfig dynamicConfig) {
		Resource res = new FileSystemResource(filePath);
		String templateFileContent = null;
		try {
			File templateFile = res.getFile();
			templateFileContent = FileUtils.readFileToString(templateFile, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return templateFileContent;
	}
	
	/**
	 * 获取生成静态模板配置
	 * 
	 * @return HtmlConfig集合
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,HtmlConfig> getHtmlConfigMap() {
		if (htmlConfigMap != null) {
			return htmlConfigMap;
		}
		File configFile = null;
		Document document = null;
		try {
			String configFilePath = SpringUtil.getRealPath(CONFIG_FILE_NAME);
			configFile = new File(configFilePath);
			SAXReader saxReader = new SAXReader();
			document = saxReader.read(configFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Element htmlConfigElement = (Element)document.selectSingleNode(HTMLCONFIG_NODE);
		Iterator<Element> iterator = htmlConfigElement.elementIterator();
		htmlConfigMap = new LinkedHashMap<String,HtmlConfig>();
	    while(iterator.hasNext()) {
	    	Element element = (Element)iterator.next();
	    	String description = element.element("description").getTextTrim();
	    	String templateFilePath = element.element("templateFilePath").getTextTrim();
	    	String htmlFilePath = element.element("htmlFilePath").getTextTrim();
	    	HtmlConfig htmlConfig = new HtmlConfig();
	    	htmlConfig.setName(element.getName());
	    	htmlConfig.setDescription(description);
	    	htmlConfig.setTemplateFilePath(templateFilePath);
	    	htmlConfig.setHtmlFilePath(htmlFilePath);
	    	htmlConfigMap.put(htmlConfig.getName(),htmlConfig);
	    }
		return htmlConfigMap;
	}
	
	/**
	 * 根据生成静态模板配置名称获取HtmlConfig对象
	 * 
	 * @return HtmlConfig对象
	 */
	public static HtmlConfig getHtmlConfig(String name) {
		return getHtmlConfigMap().get(name);
	}
	
	/**
	 * 根据HtmlConfig对象读取模板文件内容
	 * 
	 * @return 模板文件内容
	 */
	public static String readTemplateFileContent(HtmlConfig htmlConfig) {
		/*ServletContext servletContext = ServletActionContext.getServletContext();
		File templateFile = new File(servletContext.getRealPath(htmlConfig.getTemplateFilePath()));*/
		String templateFileContent = null;
		try {
			Resource res = new FileSystemResource(filePath);
			File templateFile = res.getFile();
			templateFileContent = FileUtils.readFileToString(templateFile, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return templateFileContent;
	}
	
	/**
	 * 写入模板文件内容
	 * 
	 */
	public static String writeTemplateFileContent(HtmlConfig htmlConfig, String templateFileContent) {
	/*	ServletContext servletContext = ServletActionContext.getServletContext();
		File templateFile = new File(servletContext.getRealPath(htmlConfig.getTemplateFilePath()));*/
		try {
			Resource res = new FileSystemResource(filePath);
			File templateFile = res.getFile();
			FileUtils.writeStringToFile(templateFile, templateFileContent, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return templateFileContent;
	}
	
	/**
	 * 获取邮件模板配置
	 * 
	 * @return MailConfig集合
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,MailConfig> getMailConfigMap() {
		if (mailConfigMap != null) {
			return mailConfigMap;
		}
		Document document = null;
		try {
			String configFilePath = SpringUtil.getRealPath(CONFIG_FILE_NAME);
			File configFile = new File(configFilePath);
			SAXReader saxReader = new SAXReader();
			document = saxReader.read(configFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Element mailConfigElement = (Element)document.selectSingleNode(MAILCONFIG_NODE);
		Iterator<Element> iterator = mailConfigElement.elementIterator();
		mailConfigMap = new LinkedHashMap<String,MailConfig>();
	    while(iterator.hasNext()) {
	    	Element element = (Element)iterator.next();
	    	String description = element.element("description").getTextTrim();
	    	String subject = element.element("subject").getTextTrim();
	    	String templateFilePath = element.element("templateFilePath").getTextTrim();
	    	MailConfig mailConfig = new MailConfig();
	    	mailConfig.setName(element.getName());
	    	mailConfig.setDescription(description);
	    	mailConfig.setSubject(subject);
	    	mailConfig.setTemplateFilePath(templateFilePath);
	    	mailConfigMap.put(mailConfig.getName(),mailConfig);
	    }
		return mailConfigMap;
	}
	
	/**
	 * 根据邮件模板配置名称获取MailConfig对象
	 * 
	 * @return MailConfig对象
	 */
	public static MailConfig getMailConfig(String name) {
		return getMailConfigMap().get(name);
	}
	
	/**
	 * 根据MailConfig对象读取模板文件内容
	 * 
	 * @return 模板文件内容
	 */
	public static String readTemplateFileContent(MailConfig mailConfig) {
		/*ServletContext servletContext = ServletActionContext.getServletContext();
		File templateFile = new File(servletContext.getRealPath(mailConfig.getTemplateFilePath()));*/
		String templateFileContent = null;
		try {
			Resource res = new FileSystemResource(filePath);
			File templateFile = res.getFile();
			templateFileContent = FileUtils.readFileToString(templateFile, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return templateFileContent;
	}
	
	/**
	 * 写入模板文件内容
	 * 
	 */
	public static String writeTemplateFileContent(MailConfig mailConfig, String templateFileContent) {
		/*ServletContext servletContext = ServletActionContext.getServletContext();
		File templateFile = new File(servletContext.getRealPath(mailConfig.getTemplateFilePath()));*/
		try {
			Resource res = new FileSystemResource(filePath);
			File templateFile = res.getFile();
			FileUtils.writeStringToFile(templateFile, templateFileContent, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return templateFileContent;
	}

}